package com.wodsy.phoenix.apps.index.controller;


import javax.servlet.http.HttpServletRequest;

import com.wodsy.phoenix.commons.utils.MD5Utils;
import com.wodsy.phoenix.commons.utils.RandomUtils;
import com.wodsy.phoenix.commons.utils.mail.MailSenderInfo;
import com.wodsy.phoenix.commons.utils.mail.SimpleMailSender;
import com.wodsy.phoenix.dataobject.base.PhoenixResponseWrapper;
import com.wodsy.phoenix.dataobject.base.ResponseCodeConstants;
import com.wodsy.phoenix.dataobject.userc.model.MobileCaptcha;
import com.wodsy.phoenix.dataobject.userc.model.UserBasic;
import com.wodsy.phoenix.dataobject.userc.vo.UserBasic4EmailRegister;
import com.wodsy.phoenix.dataobject.userc.vo.UserBasic4Login;
import com.wodsy.phoenix.dataobject.userc.vo.UserBasic4PhoneRegister;
import com.wodsy.phoenix.dataservice.userc.service.UserManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class UserManagementController {

    private Logger logger = LoggerFactory.getLogger(UserManagementController.class);

    // @Autowired
    private UserManagementService userManagementService;



    /**
     * 导向首页面
     *
     * @param request 请求
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(HttpServletRequest request) {


        return "/index";
    }
    /**
     * 导向首页面
     *
     * @param request 请求
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(HttpServletRequest request,Model model) {
        model.addAttribute("command",new UserBasic4Login( ));

        return "/login";
    }
    /**
     *登录
     * @param user     登录用户信息
     * @param error   错误信息
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(  @ModelAttribute("command") UserBasic4Login user,Errors error) {
        PhoenixResponseWrapper<UserBasic> result = userManagementService.login(user);
        if(result.getCode()!= ResponseCodeConstants.SUCCESS){
            error.rejectValue("unionId", "",result.getMsg());
            return "/login";

        }
        return "/login";
    }


    /**
     * 导向到注册页面
     *
     * @param request 请求
     * @return
     */
    @RequestMapping(value = "/registerByEmail", method = RequestMethod.GET)
    public String register(HttpServletRequest request,Model model) {
        model.addAttribute("command",new UserBasic4EmailRegister( ));

        return "/registerByEmail";
    }



    /**
     * 邮箱注册
     *
     * @return
     */
    @RequestMapping(value = "/registerByEmail", method = RequestMethod.POST)
    public String register(HttpServletRequest request, @ModelAttribute("command") UserBasic4EmailRegister userBasic4Register,Errors error) {
        PhoenixResponseWrapper<UserBasic4EmailRegister> result  = userManagementService.registerByEmail(userBasic4Register);

        if(result.getCode()!= ResponseCodeConstants.SUCCESS){//如果注册失败返回前台页面提示错误
            error.rejectValue("userEmail", "",result.getMsg());
            return "/registerByEmail";

        }else{
            //如果成功则给用户发送邮件
            MailSenderInfo mailInfo = new MailSenderInfo();
            mailInfo.setToAddress("subin@wodsy.com");
            String randomStr = RandomUtils.generateString(4);
            String email = userBasic4Register.getUserEmail();
            mailInfo.setContent("http://localhost:8080/checkVerificationCode?code="+randomStr+"&&email="+email+"&&random="+ MD5Utils.getMD5String(randomStr+email+RandomUtils.ENCRPT_KEY));
            SimpleMailSender.sendHtmlMail(mailInfo);
        }
        return "index";

    }

    /**
     * 邮箱验证
     *
     * @return
     */
    @RequestMapping(value = "/checkVerificationCode", method = RequestMethod.GET)
    public String checkVerificationCode(HttpServletRequest request,@RequestParam("code")String code,@RequestParam("random") String encrypt,@RequestParam("email")String email) {

        if(encrypt.equals( MD5Utils.getMD5String(code+email+RandomUtils.ENCRPT_KEY))){
            //那么验证成功，将用户的邮箱账号激活
             userManagementService.activiteByEmail(email);
        }


        return "index";

    }


    /**
     * 发送验证码
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sendMobileCaptcha", method = RequestMethod.GET)
    public PhoenixResponseWrapper<MobileCaptcha> sendMobileCaptcha(@RequestParam("mobile") long mobile) {

        PhoenixResponseWrapper<MobileCaptcha> result = userManagementService.sendMobileMsg(mobile);

        return result;

    }

    /**
     * 导向到手机注册
     *
     * @return
     */
    @RequestMapping(value = "/registerByMobile", method = RequestMethod.GET)
    public String registerByMobile(HttpServletRequest request,Model model) {

        model.addAttribute("command",new UserBasic4PhoneRegister( ));

        return "/registerByMobile";

    }

    /**
     * 手机注册
     *
     * @return
     */
    @RequestMapping(value = "/registerByMobile", method = RequestMethod.POST)
    public String registerByMobile( @ModelAttribute("command") UserBasic4PhoneRegister userBasic4PhoneRegister,Errors error){

        PhoenixResponseWrapper<UserBasic4EmailRegister> result  = userManagementService.registerByMobile(userBasic4PhoneRegister);

        if(result.getCode()!= ResponseCodeConstants.SUCCESS){//如果注册失败返回前台页面提示错误
            error.rejectValue("userTel", "",result.getMsg());
            return "/registerByMobile";

        }
        return "index";
    }



}
